<template>
  <van-nav-bar
      left-arrow
      title="新朋友"
      @click-left="onClickLeft"
  />
  <van-card
      v-for="(item, index) in newFriends" :key="item"
      :title="item.userNickname"
      :thumb="item.userHeadImg"
      :desc="item.verifyMessage"
      @click="handleApplication(index)"
  />
  <van-action-sheet @select="checkFriend" :actions="actions" cancel-text="取消" v-model:show="show" title="是否同意Ta的申请" />
</template>

<script>
import {checkFriend, searchNewFriend} from "@/api/friend";
import {router} from "@/router";
import {Toast} from "vant";

export default {
  name: "newfiend",
  data() {
    return{
      newFriends: [],
      actions: [
        {name: '同意', subname: '', color: '#00AEEF', verifyStatus: 2},
        {name: '拒绝', subname: '', color: '#EE0A24', verifyStatus: 1}
      ],
      show: false,
      newFriend: {}
    }
  },
  methods: {
    onClickLeft() {
      history.back()
    },
    handleApplication(index) {
      this.show = true
      this.newFriend = this.newFriends[index]
      this.actions[0].subname = this.newFriends[index].userNickname + '成为我的好友'
    },
    checkFriend(item) {
      let requestParam = {
        'verifyId': this.newFriend.verifyId,
        'verifyStatus': item.verifyStatus
      }
      checkFriend(requestParam).then(response => {
        if (response.code === 200) {
          Toast.success('添加成功')
          router.replace({path: '/home'})
        }
      })
    }
  },
  created() {
    let userId = JSON.parse(sessionStorage.getItem("user")).userId
    searchNewFriend(userId).then(response => {
      if (response.code === 200) {
        this.newFriends = response.data
      }
    })
  }
}
</script>

<style scoped>
.van-nav-bar {
  --van-nav-bar-background-color: rgb(0, 174, 239);
  --van-nav-bar-icon-color: rgb(0, 0, 0);
  --van-nav-bar-title-text-color: rgb(255, 255, 255);
  --van-nav-bar-height: 75px;
}

.van-card {
  --van-card-padding: 10px;
  --van-card-thumb-size: 45px;
  --van-card-thumb-magin-top: 10px;
  --van-card-thumb-border-radius: 50%;
  --van-card-font-size: 17px;
  --van-card-title-line-height: 25px;
  --van-card-price-color: #646566;
}
</style>